import { ReactNode } from 'react';
import { Navigate, Route,Routes } from 'react-router-dom/dist/index';
import { IRoute } from './types';
import NotFound from './views/404';
import { routes } from './routes';
export default function ProtectedRouter() {
    function renderRoute(routes: IRoute[]): ReactNode {
        return routes.map((route, index) => {
            const { Element, path, media } = route;
            if (Element) {
                if (media?.isDefault) {
                    return (
                        <Route index key={path} path={path} element={<Element></Element>}></Route>
                    );
                } else {
                    return (
                        <Route key={path} path={path} element={<Element></Element>}>
                            {route.children ? renderRoute(route.children) : null}
                        </Route>
                    );
                }
            }
            return null;
        });
    }
    return (
        <>
            <Routes>
                <Route path="/" element={<Navigate to="/goods"></Navigate>}></Route>
                {renderRoute(routes)}
                <Route key="*" path="*" element={<NotFound></NotFound>}></Route>
            </Routes>
        </>
    );
}
